<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="方案标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入方案标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择方案状态" clearable>
          <el-option
            v-for="dict in dict.type.ai_scheme_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ai:scheme:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ai:scheme:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ai:scheme:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ai:scheme:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schemeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="用户" align="center" prop="userName"/>
      <el-table-column label="方案标题" align="center" prop="title"/>
      <el-table-column label="方案简介" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="方案内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="方案标签" align="center" prop="tags" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ai_scheme_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="推荐标识" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ai_scheme_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="分解后节点总数" align="center" prop="totalStepCount"/>
      <el-table-column label="进行中节点数" align="center" prop="processCount"/>
      <el-table-column label="已完成节点数" align="center" prop="finishedCount"/>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ai:scheme:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ai:scheme:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户方案信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="会话标识" prop="sessionId">
          <el-input v-model="form.sessionId" placeholder="请输入会话ID"/>
        </el-form-item>
        <el-form-item label="主题" prop="topicId">
          <el-input v-model="form.topicId" placeholder="请输入主题ID"/>
        </el-form-item>
        <el-form-item label="方案标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入方案标题"/>
        </el-form-item>
        <el-form-item label="方案简介" prop="description">
          <el-input v-model="form.description" placeholder="请输入方案简介"/>
        </el-form-item>
        <el-form-item label="方案内容" prop="content">
          <!--<editor v-model="form.content" :min-height="192"/>-->
          <mavon-editor ref="md" prop="content" v-model="form.content" :subfield = "false"/>
        </el-form-item>
        <el-form-item label="方案标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入方案标签"/>
        </el-form-item>
        <el-form-item label="方案状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择方案状态" clearable>
              <el-option
                v-for="dict in dict.type.ai_scheme_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="推荐标识" prop="type">
          <el-select v-model="form.type" placeholder="请选择推荐标识" clearable>
              <el-option
                v-for="dict in dict.type.ai_scheme_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="分解后节点总数" prop="totalStepCount">
          <el-input v-model="form.totalStepCount" placeholder="请输入分解后节点总数" type="number"/>
        </el-form-item>
        <el-form-item label="进行中节点数" prop="processCount">
          <el-input v-model="form.processCount" placeholder="请输入进行中节点数" type="number"/>
        </el-form-item>
        <el-form-item label="已完成节点数" prop="finishedCount">
          <el-input v-model="form.finishedCount" placeholder="请输入已完成节点数" type="number"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {listScheme, getScheme, delScheme, addScheme, updateScheme} from "@/api/ai/scheme";
    import VueMarkdown from 'vue-markdown';

    export default {
        name: "Scheme",
        dicts: ['ai_scheme_status', 'ai_scheme_type'],
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 用户方案信息表格数据
                schemeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    userId: null,
                    sessionId: null,
                    topicId: null,
                    type: null,
                    title: null,
                    description: null,
                    content: null,
                    tags: null,
                    status: null,
                    totalStepCount: null,
                    processCount: null,
                    finishedCount: null,
                    sort: null,
                    createdAt: null,
                    updatedAt: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    userId: [
                        { required: true, message: "用户ID不能为空", trigger: "blur" }
                    ],
                    sessionId: [
                        { required: true, message: "会话ID不能为空", trigger: "blur" }
                    ],
                    topicId: [
                        { required: true, message: "主题ID不能为空", trigger: "blur" }
                    ],
                    title: [
                        {required: true, message: "方案标题不能为空", trigger: "blur"}
                    ],
                    description: [
                        {required: true, message: "方案简介不能为空", trigger: "blur"}
                    ],
                    content: [
                        { required: true, message: "方案内容不能为空", trigger: "blur" }
                    ],
                    status: [
                        {required: true, message: "状态不能为空", trigger: "change"}
                    ],
                    tags: [
                        { required: true, message: "标签不能为空", trigger: "blur" }
                    ]
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询用户方案信息列表 */
            getList() {
                this.loading = true;
                listScheme(this.queryParams).then(response => {
                    this.schemeList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    userId: null,
                    sessionId: null,
                    topicId: null,
                    type: null,
                    title: null,
                    description: null,
                    content: null,
                    tags: null,
                    status: null,
                    totalStepCount: null,
                    processCount: null,
                    finishedCount: null,
                    sort: null,
                    createdAt: null,
                    updatedAt: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "新增用户方案";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getScheme(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改用户方案";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateScheme(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addScheme(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除用户方案信息编号为"' + ids + '"的数据项？').then(function () {
                    return delScheme(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download('ai/scheme/export', {
                    ...this.queryParams
                }, `scheme_${new Date().getTime()}.xlsx`)
            }
        }
    };
</script>
